package com.ptu.config;

import com.ptu.util.JwtAuthenticationInterceptor;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

@Configuration
public class MyWebMvcConfiguration implements WebMvcConfigurer {

    /**
     * 全局CORS配置
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
            .allowedOrigins("http://localhost:5173", "http://localhost:6066") // 用逗号分隔多个
            .allowCredentials(true)
            .allowedMethods("*")
            .allowedHeaders("*");
    }

    @Resource
    private StringRedisTemplate stringRedisTemplate;

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        // 登录拦截器
//        registry.addInterceptor(new LoginInterceptor())
//                .excludePathPatterns(
//                        "/user/login",
//                        "/user/code",
//                        "/user/register",
//                        "/doc.html",       // Knife4j 文档
//                        "/webjars/**",     // Swagger 静态资源
//                        "/swagger-resources/**",
//                        "/v3/api-docs/**",
//                        "/user/upload/avatar/register", //用户注册头像
//                        "/product/list",
//                        "/product/detail",
//                        "/admin/**",
//                        "/ws/**"
//                ).order(1);
//        // token刷新拦截器
//        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
//                .addPathPatterns("/**")
//                .excludePathPatterns(
//                        "/user/login",
//                        "/user/code",
//                        "/user/register",
//                        "/user/upload/avatar/register"
//                ).order(0);
//    }

    // 解决跨域问题
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON));
        converters.add(converter);
    }

    @Autowired
    private JwtAuthenticationInterceptor jwtAuthenticationInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // JWT认证拦截器
        registry.addInterceptor(jwtAuthenticationInterceptor)
                .excludePathPatterns(
                        "/user/login",
                        "/user/code",
                        "/user/register",
                        "/doc.html",
                        "/webjars/**",
                        "/swagger-resources/**",
                        "/v3/api-docs/**",
                        "/user/upload/avatar/register",
                        "/product/list",
                        "/product/detail",
                        "/admin/user/login",
                        "/api/alipay/**",
                        "/api/chart/**",
                        "/user/captcha"
                ).order(1);
    }
}